#{extends 'main.html' /} #{set title:'Browse' /}
<head>
<script type="text/javascript">
	$(function() {
		$('#orgOptions').hide();
		$('#orgOptions').slideDown(1000);
		javascript: showOrganizationsJoined();
		$('#joinedOrgs').hide();
		javascript: showOrganizationsManaged();
		$('#managedOrgs').hide();
		javascript: showOrganizationsFollowed();
		$('#followedOrgs').hide();
	});
</script>

<!-- ---------------------------------------------------javascript for showing organizations-------------------------------------->
<script type="text/javascript">
	function showOrganizationsJoined() {
		var user = '${user}';
		$
				.getJSON(
						'@{BrowseController.browseOrganizationsJoined()}',
						{
							'userName' : user
						},
						function(data) {
							var orgs = data.joinedOrgs;
							var orgsPart = "";
							if (orgs == null || orgs == "") {
								document.getElementById("joinedOrgs").innerHTML = "No organizations to show";
							} else {
								orgsPart = "";
								var x = orgs.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									orgsPart += "<a href=\"@{Application.organizationView()}?name=" + x[i] +"\">" + x[i] + "</a> ";
									orgsPart += "<a href = '#' onclick = \"getInsideOrg('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a>";
									orgsPart += "<div id = \"org" + x[i] + "\"></div>";
								}
								document.getElementById("joinedOrgs").innerHTML = orgsPart;
							}
						});
	}
</script>

<script type="text/javascript">
	function showOrganizationsManaged() {
		var user = '${user}';
		$
				.getJSON(
						'@{BrowseController.browseOrganizationsManaged()}',
						{
							'userName' : user
						},
						function(data) {
							var orgs = data.managedOrgs;
							var orgsPart = "";
							if (orgs == null || orgs == "") {
								document.getElementById("managedOrgs").innerHTML = "No organizations to show";
							} else {
								orgsPart = "";
								var x = orgs.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									orgsPart += "<a href=\"@{Application.organizationView()}?name=" + x[i] +"\">" + x[i] + "</a> ";
									orgsPart += "<a href = '#' onclick = \"getInsideOrg('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a>";
									orgsPart += "<div id = \"org" + x[i] + "\"></div>";
								}
								document.getElementById("managedOrgs").innerHTML = orgsPart;
							}
						});
	}
</script>

<script type="text/javascript">
	function showOrganizationsFollowed() {
		var user = '${user}';
		$
				.getJSON(
						'@{BrowseController.browseOrganizationsFollowed()}',
						{
							'userName' : user
						},
						function(data) {
							var orgs = data.followedOrgs;
							var orgsPart = "";
							if (orgs == null || orgs == "") {
								document.getElementById("followedOrgs").innerHTML = "No organizations to show";
							} else {
								orgsPart = "";
								var x = orgs.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									orgsPart += "<a href=\"@{Application.organizationView()}?name=" + x[i] +"\">" + x[i] + "</a> ";
									orgsPart += "<a href = '#' onclick = \"getInsideOrg('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a>";
									orgsPart += "<div id = \"org" + x[i] + "\"></div>";
								}
								document.getElementById("followedOrgs").innerHTML = orgsPart;
							}
						});
	}
</script>

<script type="text/javascript">
	function getInsideOrg(u, org) {
		var user = u;
		var organization = org;
		$
				.getJSON(
						'@{BrowseController.showOrgContents()}',
						{
							'orgName' : organization,
							'userName' : user
						},
						function(data) {
							var topics = "";
							var entit = "";
							var ideas = "";
							var desiredOrg = "org" + organization;
							if (data != null && data.entities != "") {
								var x = data.entities.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									entit += "<a href=\"@{Application.entityViewByName}?entityName=" + x[i] +"\">" + x[i] +  "</a> ";
									entit += "<a href = '#' onclick = \"getInsideEntit('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a></p>";
									entit += "<div id = \"entit" + x[i] + "\"></div>";
								}
							} else {
								alert("No entities inside the organization");
							}

							if (data != null && data.topics != "") {
								var y = data.topics.split("&&");

								//Note here when a user clicks on a topic he should be redirected to topic's page
								for ( var j = 0; j < y.length - 1; j = j + 1) {
									var id = y[j].split("-")[0];
									var title = y[j].split("-")[1];
									topics += "<p><a href=\"@{TopicIdeaController.checkAuthorizationToViewTopic()}?topicId=" + id +"\">" + title + "</a> ";
									topics += "<a href = '#' onclick = \"getInsideTopic('"
											+ ""
											+ user
											+ "','"
											+ y[j]
											+ "')\">" + "expand" + "</a></p>";
									topics += "<div id = \"topic" + y[j] + "\"></div>";
								}
							} else {
								alert("No topics inside the organization");
							}

							if (data != null && data.ideas != "") {
								var z = data.ideas.split("&&");
								//Note here when a user clicks on a topic he should be redirected to topic's page
								for ( var k = 0; k < z.length - 1; k = k + 1) {
									var id = z[k].split("-")[0];
									var title = z[k].split("-")[1];
									ideas += "<p><a href=\"@{TopicIdeaController.checkAuthorizationToViewIdea()}?ideaId=" + id +"\">" + title + "</a></p>";
								}
							} else {
								alert("No ideas inside the organization");
							}

							document.getElementById(desiredOrg).innerHTML = "Ideas</br>" + ideas
									+ "Topics</br>" + topics + "Entities</br>"+ entit;
									$('#'+ desiredOrg+ '').hide();
									$('#'+ desiredOrg+ '').slideDown(1000);
						});
	}
</script>


<script type="text/javascript">
	function getInsideEntit(u, entit) {
		var user = String(u);
		var entity = String(entit);
		$.getJSON('@{BrowseController.showEntitContents()}', {
			'entityName' : entity,
			'userName' : user
		}, function(data) {
			var entit = "";
			var topics = "";
			var desiredEntity = "entit" + entity;
			if (data != null && data.entities != "") {
				var x = data.entities.split("&&");
				for ( var i = 0; i < x.length-1; i = i + 1) {
					entit += "<a href=\"@{Application.entityViewByName}?entityName=" + x[i] +"\">" + x[i] +  "</a> ";
					entit += "<a href = '#' onclick = \"getInsideEntit('"
							+ "" + user + "','" + x[i] + "')\">" + "expand"
							+ "</a></p>";
					entit += "<div id = \"entit" + x[i] + "\"></div>";
				}
			} else {
				alert("No entities inside the entity");
			}

			if (data != null && data.topics != "") {
				var y = data.topics.split("&&");
				for ( var j = 0; j < y.length-1; j = j + 1) {
					var id = y[j].split("-")[0];
					var title = y[j].split("-")[1];
					topics += "<p><a href=\"@{TopicIdeaController.checkAuthorizationToViewTopic()}?topicId=" + id +"\">" + title + "</a> ";
					topics += "<a href = '#' onclick = \"getInsideTopic('"
							+ "" + user + "','" + y[j] + "')\">" + "expand"
							+ "</a></p>";
					topics += "<div id = \"topic" + y[j] + "\"></div>";
				}

			} else {
				alert("No topics inside the entity");
			}

			document.getElementById(desiredEntity).innerHTML = topics + entit;
			$('#'+ desiredEntity+ '').hide();
			$('#'+ desiredEntity+ '').slideDown(1000);
		});
	}
</script>

<script type="text/javascript">
	function showOrgOption(orgDiv) {
		if (orgDiv == "joinedOrgs") {
			$('#followedOrgs').hide(1000);
			$('#managedOrgs').hide(1000);
			$('#joinedOrgs').toggle(1000);
		} else if (orgDiv == 'followedOrgs') {
			$('#joinedOrgs').hide(1000);
			$('#managedOrgs').hide(1000);
			$('#followedOrgs').toggle(1000);
		} else if (orgDiv == 'managedOrgs') {
			$('#joinedOrgs').hide(1000);
			$('#followedOrgs').hide(1000);
			$('#managedOrgs').toggle(1000);
		} else {
			$('#joinedOrgs').hide(1000);
			$('#followedOrgs').hide(1000);
			$('#managedOrgs').hide(1000);
		}
	}
</script>

<script type="text/javascript">
	function getInsideTopic(userName, topic) {
		var user = userName;
		var topicId = (topic.split("-"))[0];
		var desiredTopic = "topic" + topic;
		$
				.getJSON(
						'@{BrowseController.showTopicContents()}',
						{
							'topicId' : topicId,
							'userName' : user
						},
						function(data) {
							var ideas = data.ideas;
							var ideasPart = "";
							if (ideas == null || ideas == "") {
								document.getElementById(desiredTopic).innerHTML = "No ideas to show";
							} else {
								var y = ideas.split("&&");
								for ( var j = 0; j < y.length-1; j = j + 1) {
									var id = y[j].split("-")[0];
									var title = y[j].split("-")[1];
									ideasPart += "<p><a href=\"@{TopicIdeaController.checkAuthorizationToViewIdea()}?ideaId=" + id +"\">" + title + "</a></p>";
								}
								document.getElementById(desiredTopic).innerHTML = "";
								document.getElementById(desiredTopic).innerHTML = ideasPart;
								$('#'+ desiredTopic+ '').hide();
								$('#'+ desiredTopic+ '').slideDown(1000);
							}

						});
	}
</script>


</head>
<body>
<p id="orgOptions">
		<a href='#' onclick="showOrgOption('joinedOrgs')"> Joined
			Organizations</a> <a href='#' onclick="showOrgOption('followedOrgs')">
			Followed Organizations</a> <a href='#'
			onclick="showOrgOption('managedOrgs')"> Managed Organizations</a>
	</p>
	
<div id="joinedOrgs" value="hidden"></div>
	<div id="managedOrgs" value="hidden"></div>
	<div id="followedOrgs" value="hidden"></div>
</body>
